<?php
if (!(defined('IN_IA'))) 
{
	exit('Access Denied');
}

class Index_MxShopPage extends MobilePage
{
    public function main(){
        $this->login();
    }

    //用户登录页面/提交
	public function login() {
		global $_W, $_GPC;

        if(is_weixin() || !empty($_GPC['__mx_shop_member_session_'.$_W['uniacid']]))
        {
            header('location: '.mobileUrl());
        }

        //post提交登录
        if($_W['ispost'])
        {
            //验证令牌
            if(empty($_GPC['token']) || !$this->checkToken($_GPC['token']))
            {
                show_json(0, '参数有误');
            }

            $mobile = trim($_GPC['phone']);
            $pwd = trim($_GPC['pwd']);

            if(!is_phone($mobile))
            {
                show_json(0, '请输入正确的手机号码');
            }

            $member =m('userinfo')->getUserByMobile($mobile, array('mobile','pwd','salt'));
            if(empty($member))
            {
                show_json(0, '用户不存在');
            }
            if(md5(md5($pwd).$member['salt'])!==$member['pwd'])
            {
                show_json(0, '用户或密码错误');
            }

            /***** 发送登录请求获取token 网关登录 strat *****/
            $res = m('userinfo')->userLoginToProxy($mobile, $pwd);
            //请求结果
            if($res['code'] != 0)
            {
                show_json(0, $res['msg']);
            }
            /************** 网关登录 end ************/

            //设置登录信息
            m('account')->setLogin($member);
            show_json(1, '登录成功');
        }

        $backurl = "";
        if(!empty($_GPC['backurl']))
        {
            $backurl = $_W['siteroot'].'app/index.php?'.base64_decode(urldecode($_GPC['backurl']));
        }

        //获取令牌
        $token = $this->getToken();

		include $this->template('user/login');
	}

	//用户注册页面/提交
	public function reg()
    {
        global $_W, $_GPC;

        if(is_weixin() || !empty($_GPC['__mx_shop_member_session_'.$_W['uniacid']]))
        {
            header('location: '.mobileUrl());
        }

        //post提交注册
        if($_W['ispost'])
        {
            //验证令牌
            if(empty($_GPC['token']) || !$this->checkToken($_GPC['token']))
            {
                show_json(0, '参数有误');
            }

            $this->rf(0);
            exit();
        }

        $sendtime = $_SESSION['verifycodesendtime'];
        if(empty($sendtime) || $sendtime+60<time())
        {
            $endtime = 0;
        }else
        {
            $endtime = 60 - (time() - $sendtime);
        }

        //获取令牌
        $token = $this->getToken();

        include $this->template();
    }

    //用户修改密码页面/提交
    public function change(){
        global $_W, $_GPC;

        if(is_weixin() || !empty($_GPC['__mx_shop_member_session_'.$_W['uniacid']]))
        {
            header('location: '.mobileUrl());
        }

        //post提交修改密码
        if($_W['ispost']){

            //验证令牌
            if(empty($_GPC['token']) || !$this->checkToken($_GPC['token']))
            {
                show_json(0, '参数有误');
            }

            $this->rf(1);
            exit();
        }

        //获取短信验证码时间间隔
        $sendtime = $_SESSION['verifycodesendtime'];
        if(empty($sendtime) || $sendtime+60<time())
        {
            $endtime = 0;
        }else
        {
            $endtime = 60 - (time() - $sendtime);
        }

        //获取令牌
        $token = $this->getToken();

        include $this->template();
    }

    //Rf
    protected function rf($type)
    {
        global $_W, $_GPC;

        //  获取数据并验证
        $mobile = trim($_GPC['phone']);
        $verifycode = trim($_GPC['code']);
        $pwd = trim($_GPC['pwd']);
        $pwd2 = trim($_GPC['pwd2']);
        if(empty($mobile) || !is_phone($mobile))
        {
            show_json(0, '请输入正确的手机号码');
        }
        if(empty($verifycode))
        {
            show_json(0, '请输入验证码');
        }
        if(empty($pwd) || empty($pwd2))
        {
            show_json(0, '请输入密码');
        }
        if($pwd != $pwd2)
        {
            show_json(0, '密码不一致');
        }
        //  判断 验证码
        $key = '__mx_shop_user_verifycodesession_'.$_W['uniacid'].'_'.$mobile;
        if( !isset($_SESSION[$key]) ||  $_SESSION[$key]!==$verifycode || !isset($_SESSION['verifycodesendtime']) || $_SESSION['verifycodesendtime']+600<time())
        {
            show_json(0, '验证码错误或已过期!');
        }
        $member =m('userinfo')->getUserByMobile($mobile, array('mobile','pwd','salt'));

        if(empty($type)){
            //  注册
            if(!empty($member)){
                show_json(0, '此手机号已注册, 请直接登录');
            }
            //  获取/设置salt
            $salt = m('account')->getSalt();

            $data =array(
                'mobile'=>$mobile,
                'pwd'=>md5(md5($pwd).$salt),
                'salt'=>$salt,
                'storeid'=>0,
                'createtime'=>time()
            );

            //获取第三方用户信息
            if(!empty($_W['openid'])){
                $userinfo = m('userinfo')->getUserInfo($_W['openid']);
                $data['realname']  = empty($userinfo['realname']) ? '' : $userinfo['realname'];
                $data['gender']  = empty($userinfo['gender']) ? 0 : $userinfo['gender'];
                $data['country']  = empty($userinfo['country']) ? '' : $userinfo['country'];
                $data['province']  = empty($userinfo['province']) ? '' : $userinfo['province'];
                $data['city']  = empty($userinfo['city']) ? '' : $userinfo['city'];
                $data['area']  = empty($userinfo['area']) ? '' : $userinfo['area'];
                $data['avatar']  = empty($userinfo['avatar']) ? '' : $userinfo['avatar'];
            }

        }else
        {
            //  找回密码
            if(empty($member)){
                show_json(0, '此手机号未注册');
            }
            $salt = m('account')->getSalt();
            $data = array('salt'=>$salt,'pwd'=>md5(md5($pwd).$salt));
        }

        // 处理写入数据库
        if(empty($member))
        {
            /***** 发送注册请求 网关注册 strat *****/

            $info = array();
            //是否门店推荐
            if($_GPC['store_number'])
            {
                $store = m('store')->getStoreByNumber($_GPC['store_number'], array('mdnumber','country','province','city','area'));
                if(!empty($store))
                {
                    $info['storeid'] = $store['id'];
                    //本地数据
                    $data['storeid'] = $store['id'];
                    $data['country']  = $store['country'];
                    $data['province']  = $store['province'];
                    $data['city']  = $store['city'];
                    $data['area']  = $store['area'];
                }

            //是否存在推荐人
            }elseif(!empty($_GPC['mid']))
            {
                $uuinfo = m('userinfo')->getUserByUuid($_GPC['mid'], array('uuid','mobile'));
                if(!empty($uuinfo))
                {
                    $info['uuid'] = $uuinfo['uuid'];
                    //本地父id
                    $data['parent_uuid'] = $uuinfo['uuid'];
                }
            }

            //网关注册
            $res = m('userinfo')->userRegisterToProxy($mobile, $pwd, $info);
            //请求结果
            if($res['code'] != 0)
            {
                show_json(0, $res['msg']);
            }
            /************** 网关注册 end ************/

            //保存到本地
            $data['uuid'] = $res['data']['uuid'];
            pdo_insert('mx_shop_user', $data);
            $uid = pdo_insertid();

            //web用户注册
            if(empty($_W['openid'])){
                $openid = 'wap_user_'.$_W['uniacid'].'_'.$mobile;
                $nickname = substr($mobile,0,3).'xxxx'.substr($mobile,7,4);
                $info = array(
                    'uniacid'=>$_W['uniacid'],
                    'uid'=>$uid,
                    'openid'=>$openid,
                    'nickname'=>$nickname,
                    'gender'=>0,
                    'isuser'=>1,
                    'createtime'=>time()
                );
                pdo_insert('mx_shop_userinfo', $info);
            }

        } else
        {
            /***** 找回密码 网关start *****/


            /***** 找回密码 网关end *****/

            //更新本地数据
            $uid = $member['id'];
            pdo_update('mx_shop_user', $data, array('id'=>$uid));

        }

        unset($_SESSION[$key]);

        show_json(1, empty($type) ? "注册成功" : "密码重置成功");

    }

    //登出
    public function logout(){
        global $_W, $_GPC;
        $key = '__mx_shop_member_session_'.$_W['uniacid'];
        isetcookie($key, false, -100);
        header('location: '.mobileUrl());
        exit;
    }


}
